<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/11
 * Time: 17:29
 */
namespace app\admin\controller;
use think\File;
use think\Request;
use think\Db;

class HouseController extends CommonController
{
    /**
     * 获取房产信息
     */
    public function getData() {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $key = input('key');
        $map = [];
        if($key != ""){
            $map['house_name'] = ['like',"%" . $key . "%"];
        }
        $notice = Db::table('ew_house')
            ->alias ('h')
            ->join('ew_place p','h.house_place=p.place_id','left')
            ->field('house_id,house_name,house_img,house_introduce,house_type,house_area,house_price,place_id,place_name')
            ->where($map)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('ew_house')
            ->alias ('h')
            ->join('ew_place p','h.house_place=p.place_id','left')
            ->where($map)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json($data);
    }

    /**
     * 添加房产信息
     * @return mixed
     */
    public function addHouse(){
        $data = Db::table('ew_place')->field('place_id,place_name')->select();
        $this->assign ('data',$data);
        return $this->fetch('house/addhouse');
    }

    /**
     * 添加房产
     * @return \think\response\Json
     */
    public function add(){
        extract(input());
        $param = [];
        if($_FILES['file']['name'][0]!='') {
            $tmp = $_FILES['file']['tmp_name'];
            $names = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $info = $this->uploadFile($tmp,$names,$size);
            if($info['status'] == 200){
                $param['house_img'] =$info['msg'];
            }else{
                return json(['code'=>100,'msg'=>$info['msg']]);
            }
        }
        $type = implode(',',$type);
        $time = date('Y-m-d H:i:s');
        $param['house_name'] = $name;
        $param['house_introduce'] = $introduce;
        $param['house_type'] = $type;
        $param['house_area'] = $areas;
        $param['house_price'] = $price;
        $param['house_place'] = $place;
        $param['house_create_time'] = $time;
        $param['house_update_time'] = $time;
        $res = Db::table('ew_house')->insert($param);
        if($res){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>100,'msg'=>'添加失败']);
        }
    }

    /**
     * 修改房产页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateHouse(){
        $res = Db::table('ew_house')->where('house_id',input('id'))->find();
        if($res['house_type'] != ""){
            $type =  trim($res['house_type'],',');
            $type = explode(',',$type);
            $res['house_type'] = $type;
        }
        $data = Db::table('ew_place')->field('place_id,place_name')->select();
        $this->assign ('res',$res);
        $this->assign ('data',$data);
        return $this->fetch('house/updatehouse');
    }

    /**
     * 修改房产
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOExceptio
     *
     */
    public function update(){
        extract(input());
        $param = [];
        if($_FILES['file']['name']!='') {
            $tmp = $_FILES['file']['tmp_name'];
            $names = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $info = $this->uploadFile($tmp,$names,$size);
            if($info['status'] == 200){
                $param['house_img'] =$info['msg'];
            }else{
                return json(['code'=>100,'msg'=>$info['msg']]);
            }
        }
        $type = implode(',',$type);
        $time = date('Y-m-d H:i:s');
        $param['house_name'] = $name;
        $param['house_introduce'] = $introduce;
        $param['house_type'] = $type;
        $param['house_area'] = $areas;
        $param['house_price'] = $price;
        $param['house_place'] = $place;
        $param['house_update_time'] = $time;
        $res = Db::table('ew_house')->where('house_id',$id)->update($param);
        if($res){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>100,'msg'=>'添加失败']);
        }
    }

    /**
     * 删除房产
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delHouse(){
        $id = input('id');
        $res = Db::table('ew_house')->delete($id);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


}